// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-8-20

#include "stdafx.h"
#include ".\facepop.h"
#include "roompage.h"
#include "mainuiman.h"
#include <common/ui/edit.h>
#include <common/ui/bitmapdata.h>


IMPLEMENT_DYNAMIC(CJoyFacePop, CJoyWndContainer)

//##ModelId=44C4B5E301AA
CJoyFacePop::CJoyFacePop(void)
{
    m_name = "face_pop";
    m_skinArea = true;
    
    m_faceSel   = -1;
    m_faceRow   = 0;
    m_faceCol   = 0;
    m_faceWidth = 0;
    m_faceHeight= 0;
}

//##ModelId=44C4B5E301AC
CJoyFacePop::~CJoyFacePop(void)
{
    FreeFaces();
}

//##ModelId=44C4B5E30168
void CJoyFacePop::FreeFaces()
{
    int c = (int)m_faces.GetCount();
    for(int i = 0; i < c; ++ i)
    {
        delete m_faces[i];
    }
    m_faces.RemoveAll();
}

//##ModelId=44C4B5E301B5
bool CJoyFacePop::Create()
{
    if(!CreateEx(
        WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
        TOOLTIPS_CLASS,
        //JoyGetManager()->GetWndClass(),
        NULL,
        WS_POPUP,
        CRect(0, 0, 10, 10),
        NULL, 0))
    {
        return false;
    }

    return true;
}

//##ModelId=44C4B5E301B6
void CJoyFacePop::Show(const CPoint& pt)
{
    int x = pt.x;
    int y = pt.y;

    CRect cr;
    GetWindowRect(&cr);
    y -= cr.Height();

    MoveWindow(x, y, cr.Width(), cr.Height());
    ShowWindow(SW_SHOW);
}

//void CJoyFacePop::InsertFace(LPRICHEDITOLE lpRichOle, int nFace)
//{
//    if(nFace < 0 || nFace >= m_faces.GetCount())
//    {
//        return;
//    }
//    m_faces[nFace]->InsertTo(lpRichOle);
//}
//##ModelId=44C4B5E301B8
void CJoyFacePop::InsertFace(LPRICHEDITOLE lpRichOle, int nFace)
{
    if(nFace < 0 || nFace >= m_faces.GetCount())
    {
        return;
    }
    m_faces[nFace]->InsertTo(lpRichOle,nFace);
}

BEGIN_MESSAGE_MAP(CJoyFacePop, CJoyWndContainer)
    ON_WM_SHOWWINDOW()
    ON_WM_SETFOCUS()
    ON_WM_KILLFOCUS()
    ON_WM_ERASEBKGND()
    ON_WM_SIZE()
    ON_WM_CREATE()
END_MESSAGE_MAP()
//##ModelId=44C4B5E30169
void CJoyFacePop::CM_Mouse(MouseMessage msg, const CPoint& pt)
{
    __super::CM_Mouse(msg, pt);

    if(m_faceRow <= 0 || m_faceCol <= 0 || m_faceWidth <= 0 || m_faceHeight <= 0)
    {
        return;
    }
    if(msg == MM_Move)
    {

        int x = pt.x - m_margin.left;
        int y = pt.y - m_margin.top;
        int r = y/m_faceHeight;
        int c = x/m_faceWidth;
        if(r<0 || r>=m_faceRow || c<0 || c>=m_faceCol)
        {
            return;
        }
        int sel = r*m_faceCol+c;
        if(m_faceSel != sel)
        {
            m_faceSel = sel;
            InvalidateCtrl();
        }
    }
    else if(msg == MM_LeftDown)
    {
        if(m_faceSel < 0)
        {
            return;
        }
        SetVisible(false);
        CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
        CRichEditCtrl& rec = roomPage->m_chatEdit->GetEditWnd();
        if(rec.GetTextLength() > JoyMessage::UserChat_Max-10)
        {
            return;
        }
        CHAR szFace[16];
        sprintf(szFace, "\\%.3d", m_faceSel);
        //roomPage->m_chatEdit->GetEditWnd().ReplaceSel(szFace);
		
		IRichEditOle *pRichItem = NULL;
		::SendMessage(roomPage->m_chatEdit->GetEditWnd().m_hWnd, EM_GETOLEINTERFACE, 0, (LPARAM)&pRichItem);

		if(pRichItem)this->InsertFace(pRichItem, m_faceSel);

    }
}

//void CJoyFacePop::CM_Mouse(MouseMessage msg, const CPoint& pt)
//{
//    __super::CM_Mouse(msg, pt);
//
//    if(m_faceRow <= 0 || m_faceCol <= 0 || m_faceWidth <= 0 || m_faceHeight <= 0)
//    {
//        return;
//    }
//    if(msg == MM_Move)
//    {
//
//        int x = pt.x - m_margin.left;
//        int y = pt.y - m_margin.top;
//        int r = y/m_faceHeight;
//        int c = x/m_faceWidth;
//        if(r<0 || r>=m_faceRow || c<0 || c>=m_faceCol)
//        {
//            return;
//        }
//        int sel = r*m_faceCol+c;
//        if(m_faceSel != sel)
//        {
//            m_faceSel = sel;
//            InvalidateCtrl();
//        }
//    }
//    else if(msg == MM_LeftDown)
//    {
//        if(m_faceSel < 0)
//        {
//            return;
//        }
//        SetVisible(false);
//        CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
//        CRichEditCtrl& rec = roomPage->m_chatEdit->GetEditWnd();
//        if(rec.GetTextLength() > JoyMessage::UserChat_Max-10)
//        {
//            return;
//        }
//        CHAR szFace[16];
//        sprintf(szFace, "\\%.3d", m_faceSel);
//        roomPage->m_chatEdit->GetEditWnd().ReplaceSel(szFace);
//    }
//}

//##ModelId=44C4B5E3017C
void CJoyFacePop::CM_Draw(Graphics& graph)
{
    __super::CM_Draw(graph);
    if(m_image == NULL)
    {
        return;
    }
    if(m_faceRow <= 0 || m_faceCol <= 0 || m_faceWidth <= 0 || m_faceHeight <= 0)
    {
        return;
    }
    int w = (int)m_image->GetWidth();
    int h = (int)m_image->GetHeight();
    graph.DrawImage(m_image,
        Rect(m_margin.left, m_margin.top, w, h),
        0, 0, w, h, UnitPixel);
    if(m_faceSel >= 0)
    {
        int r = m_faceSel/m_faceCol;
        int c = m_faceSel%m_faceCol;
        int x = m_margin.left+c*m_faceWidth;
        int y = m_margin.top+r*m_faceHeight;
        Pen pen(m_hoverColor);
        graph.DrawRectangle(&pen, x, y, m_faceWidth, m_faceHeight);
    }
}

//##ModelId=44C4B5E30179
void CJoyFacePop::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
    __super::CM_LoadSkin(pSkinElem);

    m_faceRow   = pSkinElem->ReadIntD("face_row");
    m_faceCol   = pSkinElem->ReadIntD("face_col");
    m_faceWidth = pSkinElem->ReadIntD("face_width");
    m_faceHeight= pSkinElem->ReadIntD("face_height");
    m_hoverColor.SetValue(pSkinElem->ReadIntX("hover_color", 0xFF0000FF));

    if(m_faceRow <= 0 || m_faceCol <= 0 || m_faceWidth <= 0 || m_faceHeight <= 0)
    {
        return;
    }

    CString imgName = pSkinElem->ReadString("image");
    if(!imgName.IsEmpty())
    {
        m_image = JoyGetManager()->CreateImage(imgName);
    }
    if(m_image == NULL)
    {
        return;
    }

    FreeFaces();

    int w = m_faceWidth-1;
    int h = m_faceHeight-1;

    for(int r = 0; r < m_faceRow; ++ r)
    {
        int y = m_faceHeight*r+1;
        for(int c = 0; c < m_faceCol; ++ c)
        {
            int x = m_faceWidth*c+1;

            Bitmap bmp(w, h, PixelFormat32bppARGB);
            Graphics g(&bmp);
            g.DrawImage(m_image, Rect(0, 0, w, h), x, y, w, h, UnitPixel);

            CJoyBitmapData* pData = new CJoyBitmapData();
            pData->SetBitmap(&bmp);

            m_faces.Add(pData);
        }
    }

}

//##ModelId=44C4B5E301C6
void CJoyFacePop::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CJoyWnd::OnShowWindow(bShow, nStatus);

    if(bShow)
    {
        m_faceSel = -1;
    }
    else
    {
        CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
        roomPage->m_chatEdit->SetFocus();
    }

}

//##ModelId=44C4B5E301D4
void CJoyFacePop::OnSetFocus(CWnd* pOldWnd)
{
    CJoyWnd::OnSetFocus(pOldWnd);
    if(!IsVisible())
    {
        CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
        roomPage->m_chatEdit->SetFocus();
    }
}

//##ModelId=44C4B5E301D7
void CJoyFacePop::OnKillFocus(CWnd* pNewWnd)
{
    CJoyWnd::OnKillFocus(pNewWnd);
    
    CWnd* pWnd = pNewWnd;
    while(pWnd != NULL)
    {
        if(pWnd == this)
        {
            return;
        }
        pWnd = pWnd->GetParent();
    }

    ShowWindow(SW_HIDE);
}

//##ModelId=44C4B5E301DA
BOOL CJoyFacePop::OnEraseBkgnd(CDC* pDC)
{

    return CJoyWndContainer::OnEraseBkgnd(pDC);
}

//##ModelId=44C4B5E301E5
void CJoyFacePop::OnSize(UINT nType, int cx, int cy)
{
    CJoyWndContainer::OnSize(nType, cx, cy);

    CRect wr;
    GetWindowRect(&wr);
    CPoint orig(0, 0);
    ClientToScreen(&orig);

    orig.x -= wr.left;
    orig.y -= wr.top;
    HRGN h = CreateRectRgn(orig.x, orig.y, orig.x + cx, orig.y + cy);
    SetWindowRgn(h, TRUE);
}

//##ModelId=44C4B5E301EA
int CJoyFacePop::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CJoyWndContainer::OnCreate(lpCreateStruct) == -1)
        return -1;

    CJoyCtrl* pCtrl = new CJoyCtrl();
    pCtrl->Create(this, CRect(0, 0, 1, 1));

    return 0;
}
